package com.hw.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hw.constants.ConstantCode;
import com.hw.dao.UserDao;
import com.hw.exceptions.MyBaseException;
import com.hw.pojo.Users;
import com.hw.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @ClassName: UserServiceImpl
 * @Description: 实现用户业务逻辑
 * @Author: vie0134
 * @Date: 2020-12-14 19:27
 * @Version: 1.0
 **/
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;

    @Override
    public PageInfo<Users> findAllUser(int pageNum, int pageSize, String username, String status) {
        PageHelper.startPage(pageNum, pageSize);

        try {
            List<Users> allUser = userDao.findAllUser(username, status);
            allUser.forEach(users -> {users.setPassword(null);});
            return new PageInfo<>(allUser);
        } catch (Exception e) {
            e.printStackTrace();
        }
        throw new MyBaseException("查询异常", ConstantCode.ERROR);
    }

    @Override
    public Users findUserById(Integer id) {
        if (id == null || id == 0) {
            throw new MyBaseException("没有传递id或id格式错误", ConstantCode.DATA_NOT_TRANSFER);
        }
        try {
            Users user = userDao.findUserById(id);
            if (user != null) {
                return user;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        throw new MyBaseException("没有对应用户信息", ConstantCode.ERROR);
    }

    @Override
    public Users login(String username, String password) {
        if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password)) {
            throw new MyBaseException("没有传递用户或密码", ConstantCode.DATA_NOT_TRANSFER);
        }
        Users user = userDao.findUserByUsername(username);
        String md5DigestAsHex = DigestUtils.md5DigestAsHex((username + password).getBytes());
        if (user == null || "0".equalsIgnoreCase(user.getStatus()) || !md5DigestAsHex.equalsIgnoreCase(user.getPassword())) {
        throw new MyBaseException("用户名或密码错误", ConstantCode.USERNAME_PASSWORD_ERROR);
        }
        if ("2".equalsIgnoreCase(user.getStatus())) {
            throw new MyBaseException("用户被禁用", ConstantCode.USERNAME_PASSWORD_ERROR);
        }
        return user;
    }

    @Override
    public void addUser(Users users) {
        if (users == null || StringUtils.isEmpty(users)) {
            throw new MyBaseException("数据不为空", ConstantCode.DATA_NOT_TRANSFER);
        }
        Integer id = userDao.findUserIdByUsername(users.getUsername());
        if (id != null) {
            throw new MyBaseException("用户存在", ConstantCode.USER_EXIST);
        }
        try {
            users.setPassword(DigestUtils.md5DigestAsHex((users.getUsername() + users.getPassword()).getBytes()));
            userDao.addUser(users);
            return;
        } catch (Exception e) {
            e.printStackTrace();
        }
        throw new MyBaseException("添加失败", ConstantCode.ERROR);
    }

    @Override
    public void updateUser(Users users) {
        if (users == null || StringUtils.isEmpty(users)) {
            throw new MyBaseException("数据不为空", ConstantCode.DATA_NOT_TRANSFER);
        }
        try {
            userDao.updateUser(users);
            return;
        } catch (Exception e) {
            e.printStackTrace();
        }
        throw new MyBaseException("修改失败", ConstantCode.ERROR);
    }

    @Override
    public void deleteUserById(Integer id) {
        if (id == null) {
            throw new MyBaseException("没有传递id", ConstantCode.DATA_NOT_TRANSFER);
        }
        try {
            userDao.deleteUserById(id);
            return;
        } catch (Exception e) {
            e.printStackTrace();
        }
        throw new MyBaseException("删除失败", ConstantCode.ERROR);
    }

    @Override
    public void deleteUserByIds(List<String> ids) {
        if (ids == null || ids.size() == 0) {
            throw new MyBaseException("没有传递id", ConstantCode.DATA_NOT_TRANSFER);
        }
        try {
            userDao.deleteUserByIds(ids);
            return;
        } catch (Exception e) {
            e.printStackTrace();
        }
        throw new MyBaseException("删除失败", ConstantCode.ERROR);
    }
}
